'use strict';
module.exports = {
	/**
	 * 检查最新的invite_code和tfid,如果有变化就更新用户信息
	 */
	main: async (event) => {
		const db = uniCloud.database();

		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, _ } = util;
		let { uid, invite_code, tfid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		
		// 查询代理商的详细信息，进而获取机构id
		let agent_info = await db.collection('uni-id-users').doc(invite_code).get()
		let middleman_id = 0;
		if (agent_info.data.length == 1) {
			middleman_id = agent_info.data[0].middleman_id;
		}
		
		let update = await db.collection('uni-id-users').doc(uid).update({
			middleman_id: middleman_id,
			invite_code: invite_code,
			tfid: tfid
		})

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
